//
//  MapImageViewController.m
//  markmap
//
//  Created by 甘世清 on 2024/3/30.
//

#import "MapImageViewController.h"

#import "MapDetailCollectionViewCell.h"
@interface MapImageViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

@end

@implementation MapImageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.zx_navTitle = SQNSLocalString(@"图片");
}
- (void)SQ_UIViewForSelfView{
    
    [self.collectionView registerClass:[MapDetailCollectionViewCell class] forCellWithReuseIdentifier:@"MapDetailCollectionViewCell"];
    [self.view addSubview:self.collectionView];
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.view);
        make.top.equalTo(self.zx_navBar.mas_bottom);
    }];
    [self SQ_collectViewAddRefresh];
    [self SQ_Header];
}
- (void)SQ_Header{
    NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
    if ([self.model.userid isNotEmpty]) {
        [dic setObject:self.model.userid forKey:@"pid"];
    }
    [dic setObject:@"1" forKey:@"current"];
    [dic setObject:@"10" forKey:@"size"];
    [HttpTool post:Url11 paramDic:dic successBlock:^(BaseModel * _Nonnull httpModel) {
        NSDictionary * data = [[NSDictionary alloc]initWithDictionary:httpModel.data];
        NSArray * moarray = [data objectForKey:@"records"];
        NSArray * array = [PointModel mj_objectArrayWithKeyValuesArray:moarray];
        [self.jsonArray removeAllObjects];
        [self.jsonArray addObjectsFromArray:array];
        [self.collectionView reloadData];
        [self.collectionView.mj_header endRefreshing];
    } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
        [self.collectionView.mj_header endRefreshing];
    }];
}
- (void)SQ_Foother{
    NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
    [dic setObject:[NSString stringWithFormat:@"%d",self.page+1] forKey:@"current"];
    [dic setObject:@"20" forKey:@"size"];
    [HttpTool post:Url11 paramDic:dic successBlock:^(BaseModel * _Nonnull httpModel) {
        self.page ++;
        NSDictionary * data = [[NSDictionary alloc]initWithDictionary:httpModel.data];
        NSArray * array = [PointModel mj_objectArrayWithKeyValuesArray:[data objectForKey:@"records"]];
        [self.jsonArray addObjectsFromArray:array];
        [self.collectionView reloadData];
        [self.collectionView.mj_footer endRefreshing];
    } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
        [self.collectionView.mj_footer endRefreshing];
    }];
}
#pragma mark UICollectionViewDelegate,UICollectionViewDataSource
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.jsonArray.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView*)collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath{
    MapDetailCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MapDetailCollectionViewCell" forIndexPath:indexPath];
    PointModel * model = [self.jsonArray objectAtIndex:indexPath.item];
    [cell.bgImage sd_setImageWithURL:SQURLImage(model.url)];
  return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
   
    return CGSizeMake((SCREEN_WIDETH-40)/3.f-1, 140);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 10;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 0;
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(10, 10, 0, 10);
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    MapDetailCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MapDetailCollectionViewCell" forIndexPath:indexPath];
    NSMutableArray * dataArray = [NSMutableArray new];
    [self.jsonArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        PointModel * model = (PointModel*)obj;
        YBIBImageData * data = [YBIBImageData new];
        data.imageURL = SQURLImage(model.url);
         data.projectiveView = cell;
        [dataArray addObject:data];
    }];
    YBImageBrowser * brow = [YBImageBrowser new];
    brow.dataSourceArray = dataArray;
    brow.currentPage =  indexPath.item;
    [brow show];
}

@end
